package net.ksic.uridongne;

import java.util.Timer;
import java.util.TimerTask;

import net.ksic.common.LoginManager;
import net.ksic.common.PreferenceManager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class IntroActivity extends Activity {

	TextView introView = null;

	PreferenceManager prefrenceManager;
	LoginManager loginManager;
	boolean isLogin = false;
	
	private TimerTask mTask;
	private Timer mTimer;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_intro);
		
		introView = (TextView) findViewById(R.id.introView); 
		
		prefrenceManager = new PreferenceManager(getApplicationContext());
		loginManager = new LoginManager(getApplicationContext());

		mTask = new TimerTask() {
			public void run(){
				//callContentsActivity(); // contents용 test
				checkInit();
			}
		};
		
		mTimer = new Timer();
		
		mTimer.schedule(mTask, 1000);	
	}

	public void checkInit() {

		Thread thread = new Thread() {

			public void run() {
				if (prefrenceManager.getLoginId().equals("") == false
						&& prefrenceManager.getLoginSessionKey().equals("") == false) 
				{
					if (loginManager.LoginBySessionKey()) // 로그인 성공 -> 메인 페이지 호출
					{
						isLogin = true;
						callMapActivity();

					} else // 로그인 실패 -> 로그인 페이지 호출
					{
						isLogin = false;
						callLoginActivity();
					}
				} else // 환경 설정에 키값 없는 경우 로그인 페이지로
				{
					isLogin = false;
					callLoginActivity();
				}
			}
		};

		thread.start();
	}

	public void callLoginActivity() {

		Intent intent = new Intent(this, LoginActivity.class);
		startActivity(intent);
		
		finish();
	}

	public void callMapActivity() {

		Intent intent = new Intent(this, MapActivity.class);
		startActivity(intent);
		
		finish();
	}
}
